<?php
/**
 * @title 3011. 判断一个数组是否可以变为有序
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @return Boolean
     */
    function canSortArray($nums) {
        /**
         * @since 2024-01-24 上个区间的数字最大值，当前区间的数字最大值
         * @since 2024-01-24 上个区间的二进制数位1的数量，当前区间的数字最大值
         */
        $prevRangeMaxNumber = $currentRangeMaxNumber = 0;
        $prevRangeBit1Count = $currentRangeBit1Count = -1;

        /**
         * @since 2024-01-24 遍历数组nums
         */
        for($i=0; $i<count($nums); $i++){
            $num = $nums[$i];

            /**
             * @since 2024-01-24 数字num的二进制数位1的数量
             */
            $bit1Count = 0;
            while($num > 0){
                $bit1Count += $num & 1;
                $num = $num>>1;
            }
            $num = $nums[$i];

            /**
             * @since 2024-01-24 和当前区间的数位1数量是否一致
             * 1. 一致，更新最大值
             * 2. 不一致，开启新区间
             */
            if($bit1Count == $currentRangeBit1Count){
                $currentRangeMaxNumber = max($currentRangeMaxNumber, $num);
            } else {
                $prevRangeMaxNumber = $currentRangeMaxNumber;
                $prevRangeBit1Count = $currentRangeBit1Count;

                /**
                 * @since 2024-01-24 新区间
                 */
                $currentRangeMaxNumber = $num;
                $currentRangeBit1Count = $bit1Count;
            }

            /**
             * @since 2024-01-24 num小于上个区间最大值，区间有交叉，无法有序
             */
            if($num < $prevRangeMaxNumber){
                return false;
            } else {}
        }

        /**
         * @return Boolean
         */
        return true;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.24_6.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
